package com.example.springBoot.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.springBoot.common.BaseController;
import com.example.springBoot.dao.ArticleBodyDao;
import com.example.springBoot.dao.ArticleDao;
import com.example.springBoot.dao.ArticleTypeDao;
import com.example.springBoot.dao.UserDao;
import com.example.springBoot.domain.Article;
import com.example.springBoot.domain.ArticleBody;
import com.example.springBoot.domain.ArticleType;
import com.example.springBoot.domain.User;

@Controller
public class ArticleController extends BaseController {

	@Autowired
	private ArticleDao articleDao;

	@Autowired
	private UserDao userDao;

	@Autowired
	private ArticleTypeDao articleTypeDao;

	@Autowired
	private ArticleBodyDao articleBodyDao;

	@RequestMapping("/admin/findallarticle")
	public String findallarticle(Map<String, Object> map,
			@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC) Pageable pageable) {
		Page<Article> articlelist = articleDao.findAll(pageable);
		map.put("articlelist", articlelist);
		return "admin/article/list";
	}

	@RequestMapping("/admin/addarticle")
	public String addarticle(Article article, Map<String, Object> map) {
		article = new Article();
		List<ArticleType> typelist = articleTypeDao.findBytypeStatus(1);
		map.put("typelist", typelist);
		return "admin/article/add";
	}

	@RequestMapping("/admin/savearticle")
	public String savearticle(Article article, HttpSession session) {
		if (article.getId() == null) {
			// 保存文章内容
			ArticleBody articleBody = new ArticleBody();
			articleBody.setArticleBody(article.getArticleBody().getArticleBody());
			articleBodyDao.save(articleBody);
			if (articleBody != null) {
				Date date = new Date();
//				DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
//				String formattedDate = dateFormat.format(date);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
				String formatdate = sdf.format(date);
				// 根据登陆用户Id获取用户
				Long userid = (Long) session.getAttribute("userId");
				User user = userDao.findByid(userid);
				Article sqlarticle = new Article();
				sqlarticle.setArticleAbstract(article.getArticleAbstract());
				sqlarticle.setArticleBody(articleBody);
				sqlarticle.setArticleIcon(article.getArticleIcon());
				sqlarticle.setArticleName(article.getArticleName());
				sqlarticle.setArticleType(article.getArticleType());
				sqlarticle.setRegTime(formatdate);
				sqlarticle.setRecommend(article.getRecommend());
				sqlarticle.setUser(user);
				articleDao.save(sqlarticle);
			}
		} else {
			System.out.println(article.getRecommend());
			Article sqlarticle = articleDao.findById(article.getId());
			ArticleBody articleBody = sqlarticle.getArticleBody();
			articleBody.setArticleBody(article.getArticleBody().getArticleBody());
			articleBodyDao.save(articleBody);
			sqlarticle.setArticleAbstract(article.getArticleAbstract());
			sqlarticle.setArticleIcon(article.getArticleIcon());
			sqlarticle.setArticleName(article.getArticleName());
			sqlarticle.setArticleType(article.getArticleType());
			sqlarticle.setRecommend(article.getRecommend());
			articleDao.save(sqlarticle);
		}
		return "redirect:/admin/findallarticle";
	}

	@RequestMapping("/admin/updatearticle/{id}")
	public String updatearticle(@PathVariable Long id, Map<String, Object> map, Article article) {
		article = articleDao.findById(id);
		List<ArticleType> typelist = articleTypeDao.findBytypeStatus(1);
		map.put("typelist", typelist);
		map.put("article", article);
		return "admin/article/update";
	}

	@RequestMapping("/admin/removearticle/{id}")
	public String removearticle(@PathVariable Long id, HttpSession session) {
		Article article = articleDao.findById(id);
		Long userid = (Long) session.getAttribute("userId");
		// 判断删除文章是否属于当前登录用户
		if ((article != null) && (article.getUser().getId().equals(userid))) {
			System.out.println("userid:" + article.getUser().getId() + "sessionid:" + userid);
			articleDao.delete(article);
		}
		return "redirect:/admin/findallarticle";
	}

}
